//
//  MyTabBarController.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/24.
//  Copyright © 2017年 maq. All rights reserved.
//

#import "HomeViewController.h"
#import "ProductListController.h"
#import "MyViewController.h"
#import "SetViewController.h"
#import "CKPuslichTool.h"
#import "UIColor+NN.h"
#import "YKMyselfTool.h"
#import "TransactionController.h"
#import "UIImage+Extension.h"
#import "MyNavigationController.h"
#import "MyTabBarController.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "MSCustomTabBar.h"
#import "PicPassController.h"
#import "UserModel.h"
#import "MakeAppointmentController.h"

@interface MyTabBarController ()<MSTabBarViewDelegate>

@end

@implementation MyTabBarController
#pragma mark - Private Methods

// 添加全部的 childViewcontroller
- (void)addAllChildViewController
{
    HomeViewController *home = (HomeViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Home" viewID:@"HomeViewController"];
    [self addChildViewController:home title:@"首页" imageNamed:@"Home"];
    

    ProductListController *category = (ProductListController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Product" viewID:@"ProductListController"];
    [self addChildViewController:category title:@"在售产品" imageNamed:@"tabBar_activity"];
    
    MyViewController *my = (MyViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"My" viewID:@"MyViewController"];
    [self addChildViewController:my title:@"我的账户" imageNamed:@"tabBar_find"];
    
    TransactionController *tras= (TransactionController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Transaction" viewID:@"TransactionController"];
    [self addChildViewController:tras title:@"我的交易" imageNamed:@"tabBar_activity"];
    
    SetViewController *set = (SetViewController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Setting" viewID:@"SetViewController"];
    [self addChildViewController:set title:@"设置" imageNamed:@"tabBar_mine"];
}

// 添加某个 childViewController
- (void)addChildViewController:(UIViewController *)vc title:(NSString *)title imageNamed:(NSString *)imageNamed
{
    MyNavigationController *nav = [[MyNavigationController alloc] initWithRootViewController:vc];
    // 如果同时有navigationbar 和 tabbar的时候最好分别设置它们的title
    vc.navigationItem.title = title;
    //    nav.tabBarItem.title = title;
    //    nav.tabBarItem.image = [UIImage imageNamed:imageNamed];
    
    [self addChildViewController:nav];
}
#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    //监听通知事件
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(goToOtherView:)
                                                 name:@"goToOtherView"
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeNumLabelText:) name:@"changeNumLabelText" object:nil];

    // 利用 KVC 来使用自定义的tabBar；
    MSCustomTabBar *tabBar = [[MSCustomTabBar alloc] init];
    tabBar.tabBarView.viewDelegate = self;
    tabBar.tabBarView.tabVc = self;
    [self setValue:tabBar forKey:@"tabBar"];
//    tabBar.tabBarView
    [self addAllChildViewController];
   
}
- (void)changeNumLabelText:(NSNotification *)info{
    NSDictionary *dic = [info userInfo];
    MSCustomTabBar *bar =(MSCustomTabBar *)self.tabBar;
    [bar.tabBarView setNumberText:dic[@"num"]];
}
//改变tabar选中
- (void)goToOtherView:(NSNotification *)info
{
    NSDictionary *_dic = [info userInfo];
    int selectIndex = [[_dic objectForKey:@"index"] intValue];
    MSCustomTabBar *bar =(MSCustomTabBar *)self.tabBar;
    [bar.tabBarView setSelectIndex:selectIndex];
    self.selectedIndex = selectIndex;
}

- (void)msTabBarView:(MSTabBarView *)view didSelectItemAtIndex:(NSInteger)index
{
//    if (index==1) {
//        //    if ([[UserModel getUser].Accountstatus intValue]==2) {
//        //判断临时客户，去预约理财师
//        MakeAppointmentController *Login = (MakeAppointmentController *)[CKPuslichTool getViewControllerFromStroyBoard:@"Product" viewID:@"MakeAppointmentController"];
//        [((MyNavigationController *)self.selectedViewController) pushViewController:Login animated:YES];
//        return;
//        //    }
//    }

    
    
    
    // 切换到对应index的viewController
    self.selectedIndex = index;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
